greedy math strings *1300

Please click on ads to support us..

Python Code:

class C():
    def __init__(self, value, index):
        self.value = value
        self.index = index


n = input()

a = []
num = int(n[len(n)-1])

for i in range(len(n)):
    if int(n[i])%2 == 0:
        c = C(int(n[i]), i)
        a.append(c)

c = 1

if a == []:
    print(-1)
    c = -1
else:
    for i in a:
        if num>i.value:
            print(n[0:i.index]+str(num)+n[i.index+1:len(n)-1]+str(i.value))
            c = -1
            break


if c != -1:
    i = a[len(a)-1]
    print(n[0:i.index] + str(num) + n[i.index + 1:len(n) - 1] + str(i.value))

C++ Code:

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h> 
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
 

void solve(){
    string s;
    cin>>s;
    int n = sz(s);
    bool key=true;
    forn(i,n)
    {
        if((s[i]-'0')%2!=1)
        {
            key=false;
            break;
        }
    }
    if(key)
    {
        cout<<-1<<endl;
        return;
    }
    int r=s[n-1]-'0';
    forn(i,n)
    {
        if((s[i]-'0')%2==0)
        {
            int l=(s[i]-'0');
            if(l<r)
            {
                swap(s[i],s[n-1]);
                cout<<s<<endl;
                return;
            }
        }
    }
    rforn(i,n-1)
    {
        if((s[i]-'0')%2==0)
        {
            swap(s[i],s[n-1]);
            cout<<s<<endl;
            return;
        }
    }
}
int main()
{
    fast_cin();
    ll t = 1;
    // cin >> t;
    for(int it=1;it<=t;it++) {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know
1672A - Log Chopping
300A - Array
48D - Permutations
677C - Vanya and Label
1583B - Omkar and Heavenly Tree
1703C - Cypher
1511C - Yet Another Card Deck
1698A - XOR Mixup
1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper